#!/usr/bin/env python 
# encoding: utf-8 
# @Author: Jacqueline
# @file: 04JS脚本.py
# @time: 2021/2/23 11:20
''''''

'''
日期选择框 js改
'''
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.implicitly_wait(3)
driver.get('https://www.12306.cn/index/')
# 输入出发地和目的地
from_addr  = driver.find_element_by_id('formStationText')
from_addr.clear()
driver.find_element_by_id('fromStationText').send_keys('北京')
driver.find_element_by_id('fromStationText').send_keys(Keys.ENTER)
to_addr  = driver.find_element_by_id('formStationText')
to_addr.clear()
driver.find_element_by_id('toStationText').send_keys('上海')
driver.find_element_by_id('toStationText').send_keys(Keys.ENTER)


# 勾选学生和高铁动车
driver.find_element_by_xpath('//li[@id="isStudentDan"]/i').click()
driver.find_element_by_xpath('//li[@id="isHighDan"]/i').click()

# 定位日期属性
date_ele = driver.find_element_by_id('train_date')

# 在
js = 'document.getElementById("train_date").value= "2021-02-22"'


# 去掉只读属性，再写入值
js1 = 'document.getElementById("train_date").readOnly = false'
js3 = 'arguments[0].readOnly = false'
driver.execute_script(js,date_ele)
date_ele.send_keys('2021-02-24')

# 执行js代码之后，在执行其他的selenium操作元素，必须加强制等待，多给执行的时间
time.sleep(2)
# 用来接收js后的参数，以列表保存
js2 = 'arguments[0].value = arguments[1]'
driver.execute_script(js,date_ele,'2021-01-31')

#执行js代码
driver.execute(js)
# 清空原内容
date_ele.clear()
# 输入内容
date_ele.send_keys('2021-02-22')

js_all = """
    arguments[0].value = '长沙';
    arguments[1].value = '上海';
    arguments[2].value = '2020-02-24';
"""

